package com.skin.admin.manager;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.skin.admin.entity.Userrole;
import com.skin.admin.entity.Users;
import com.skin.admin.serviceImpl.UserroleServiceImpl;
import com.skin.admin.serviceImpl.UsersServiceImpl;
import com.skin.configuration.shiro.ShiroLoad;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by Administrator on 2018/7/20 0020.
 */
@Component
public class UsersManager{

    protected static final Logger log = LoggerFactory.getLogger(UsersManager.class);


    @Autowired
    private UsersServiceImpl usersService;

    @Autowired
    private UserroleServiceImpl userroleService;



    /**
     * 分页
     * @param page
     * @param size
     * @return
     */
    public Page<Users> findAll(int page,int size){
        return usersService.selectPage(new Page<Users>(page,size),new EntityWrapper<Users>().ne("name","admin"));
    }


    public Users findBy(String colum,String name){
       return  usersService.selectOne(new EntityWrapper<Users>().eq(colum,name ));
    }

    @Transactional
    public void deleteById(int id){
        //删除角色关联表
        userroleService.delete(new EntityWrapper<Userrole>().eq("userid",id));
        usersService.deleteById(id);
    }

    public void edit(Users users){
        if(users!=null){
            usersService.updateById(users);
        }
    }
    public void insert(Users users){
        if(users!=null){
            List<Users> size=usersService.selectList(new EntityWrapper<Users>().eq("name",users.getName()));
            if(size.size()>0){
                log.info("存在用户:"+size+" 条");
            }else{
                usersService.insert(users);
            }
        }
    }

    /**
     * 给用户分配角色
     */
    @Transactional(rollbackFor = Exception.class)
    public int userroleconfig(String userid ,String roleid){
        try{
            Users users=usersService.selectById(userid);
            if(users!=null){
                userroleService.delete(new EntityWrapper<Userrole>().eq("userid",userid));
                String[] roleidarr =roleid.split(",");
                for(int i=0;i<roleidarr.length;i++){
                    Userrole userrole=new Userrole();
                    userrole.setRoleid(Integer.parseInt(roleidarr[i]));
                    userrole.setUserid(Integer.parseInt(userid));
                    userroleService.insert(userrole);
                }
                return 1;
            }
        }catch (Exception ex){
             log.error(ex.getMessage().toString());
             throw  new RuntimeException();
            //TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚
        }
        return 0;
    }
}
